import time

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
from Lactationer_Slave_Selenium.Proxy import create_proxyauth_extension
from Lactationer_Slave_Selenium.redisClient import redisClient

# 代理设置
proxyauth_plugin_path = create_proxyauth_extension(
    proxy_host="http-dyn.abuyun.com",
    proxy_port=9020,
    proxy_username="H7790120C0414TND",
    proxy_password="BA74FBC00340C7A3"
)
co = Options()
prefs = {"profile.managed_default_content_settings.images":2}
co.add_experimental_option("prefs",prefs)
co.add_argument("--start-maximized")
co.add_argument("--Proxy-Switch-Ip=yes")
co.add_extension(proxyauth_plugin_path)
co.add_argument('lang=zh_CN.UTF-8')
co.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
driver = webdriver.Chrome(chrome_options=co, executable_path='/Users/liyang/Applications/chromedriver')
# 查看本机ip，查看代理是否起作用
# driver.get("http://httpbin.org/ip")
# print(driver.page_source)
browser = driver


r = redisClient()
while True:
    detail_url = r.getEffectiveUrl
    if detail_url is not None:
            browser.get(detail_url)
            button = browser.find_element_by_class_name('displayphonenumber')
            if button is not None:
                button.click()
                time.sleep(2)
                # phone
                phone = browser.find_element_by_class_name("show_noauth_pop")
                # provide
                provide = browser.find_element_by_xpath("//*[@id='wrapper']/div[4]/div[2]/ul/li[1]/div")
                # address
                address = browser.find_element_by_xpath("//*[@id='wrapper']/div[4]/div[2]/ul/li[2]/div/p")
                print(phone.text)
                print(provide.text)
                print(address.text)
            else:
                print('请求失败')
driver.close()